package com.apexedu.portal.controller;

import com.apexedu.framework.dao.BaseDao;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.identity.IdConstants;
import com.apexedu.identity.entity.TSysUser;
import com.apexedu.portal.constant.ActionConstant;
import com.apexedu.portal.constant.Constants;
import com.apexedu.portal.entity.TMessage;
import com.apexedu.portal.service.impl.MessageServiceImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 门户消息提醒控制类
 * 1.门户本身的信息提醒
 * 2.来自其他业务系统的信息提醒
 * 3.广播信息提醒
 * 4.后期支持站内信
 * Created on 2015.08.16
 */
@Controller
public class MessageController extends BaseController {
    private static final Log LOG = LogFactory.getLog(MessageController.class);
    @Autowired
    BaseDao baseDao;
    @Resource
    private MessageServiceImpl service;

    /**
     * 消息列表主界面
     *
     * @return
     */
    @RequestMapping(value = "/m/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request, Pager pager, ModelMap model) {
        try {
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            String userid = user.getUserloginid();
            if (user == null) {
                throw new Exception("会话已经过期或未登录！");
            }

            //初始化代办流程（从LiveBOS的流程--->门户的流程表）
            service.flushWorkflowFromLiveBOS2Portal(user.getUserloginid(), ActionConstant.Process_Charge);
            //初始化待办消息（从门户的流程表--->门户的消息表）
            service.flushMessageFromPortalWorkflow2PortalMessage(user.getUserloginid(), ActionConstant.Process_Charge);

            if (Constants.ProjectInfo.PROJECT_CODE.equals("shd")) {
                List<TMessage> ls_oa = getMessage(request, user, "unread", "oa", 5);
                List<TMessage> ls_hr = getMessage(request, user, "unread", "hr", 5);
                request.setAttribute("ls_oa", ls_oa);
                request.setAttribute("ls_hr", ls_hr);
            }
            service.loadMessage(user, pager, model);

            if (Constants.ProjectInfo.PROJECT_CODE.equals("shd")) {
                return "portal/shd/message/index";
            } else if (Constants.ProjectInfo.PROJECT_CODE.equals("sdyz")) {
                return "portal/sdyz/message/index";
            } else {
                return "portal/shmy/message/index";
            }

        } catch (Exception e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 消息详情页面
     *
     * @param messagewid 信息wid
     * @return
     */
    @RequestMapping(value = "/m/detail/{messagewid}", method = RequestMethod.GET)
    public String detail(HttpServletRequest request, ModelMap model, @PathVariable String messagewid) {
        try {
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            List<TMessage> ls_oa = getMessage(request, user, "unread", "oa", 5);
            List<TMessage> ls_hr = getMessage(request, user, "unread", "hr", 5);
            request.setAttribute("ls_oa", ls_oa);
            request.setAttribute("ls_hr", ls_hr);
            if (user == null) {
                throw new Exception("会话已经过期或未登录！");
            }

            service.loadDetail(user, model, messagewid);

            return "portal/shd/message/detail";
        } catch (Exception e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取没读信息条数
     *
     * @return 结果
     */
    @RequestMapping(value = "/m/unreadnum", method = RequestMethod.POST)
    public
    @ResponseBody
    String unreadnum(HttpServletRequest request) {
        try {
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            if (user == null)
                return "unlogoin"; //未登录
            return service.unread(user);
        } catch (Exception e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取没读信息
     *
     * @return 结果
     */
    @RequestMapping(value = "/m/unreadlist", method = RequestMethod.POST)
    public
    @ResponseBody
    String unreadlist(HttpServletRequest request) {
        try {
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            if (user == null)
                return "unlogoin"; //未登录
            return service.unreadlist(user);
        } catch (Exception e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 信息已读
     *
     * @return 结果
     */
    @RequestMapping(value = "/m/read/{messagewid}", method = RequestMethod.POST)
    public
    @ResponseBody
    String unreadnum(HttpServletRequest request, @PathVariable String messagewid) {
        try {
            TSysUser user = (TSysUser) request.getSession().getAttribute(IdConstants.SESSION_USER);
            if (user == null)
                return "unlogoin"; //未登录
            //标记为已读
            DBUtil.executeSQL("UPDATE T_MESSAGE SET zt='read' WHERE USERID=? AND WID=?", user.getUserloginid(), messagewid);
            return "已更新";
        } catch (Exception e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }

}
